package com.ktgj.customer.util;

import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chengshifei on 2019/7/11.
 */
public class StringUtil {
    static final String METHOD_SET = "set";
    static final String METHOD_GET = "get";
    private static char[] RANDOM_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

    public StringUtil() {
    }

    public static String replaceBlank(String str) {
        String dest = "";
        if(str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }

        return dest;
    }

    public static String[] splitStr2Array(String source, String spliter) {
        String regex = spliter;
        if(spliter.equals("?") || spliter.equals("*") || spliter.equals(")") || spliter.equals("(") || spliter.equals("{") || spliter.equals("$") || spliter.equals("+") || spliter.equals(".") || spliter.equals("|")) {
            regex = "[" + spliter + "]";
        }

        return source.split(regex);
    }

    public static String byte2Hex(byte[] srcBytes) {
        StringBuilder hexRetSB = new StringBuilder();
        byte[] arr$ = srcBytes;
        int len$ = srcBytes.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            byte b = arr$[i$];
            String hexString = Integer.toHexString(255 & b);
            hexRetSB.append(hexString.length() == 1?Integer.valueOf(0):"").append(hexString);
        }

        return hexRetSB.toString();
    }

    public static byte[] hex2Bytes(String source) {
        byte[] sourceBytes = new byte[source.length() / 2];

        for(int i = 0; i < sourceBytes.length; ++i) {
            sourceBytes[i] = (byte)Integer.parseInt(source.substring(i * 2, i * 2 + 2), 16);
        }

        return sourceBytes;
    }

    public static String formatString(String text) {
        return text == null?"":text;
    }

    public static boolean isNumberSepString(String value, String seperator) {
        boolean result = true;
        String[] arr = value.split(seperator);

        for(int i = 0; i < arr.length; ++i) {
            if(!isNumeric(arr[i])) {
                return false;
            }
        }

        return result;
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    public static String upperFirstChar(String string) {
        StringBuilder sb = new StringBuilder();
        sb.append((char)(string.charAt(0) - 32)).append(string.substring(1));
        return sb.toString();
    }

    public static String genGetMethodName(String fieldName) {
        StringBuilder sb = new StringBuilder();
        sb.append("get").append(upperFirstChar(fieldName));
        return sb.toString();
    }

    public static String randomString(int length) {
        char[] randBuffer = new char[length];

        for(int i = 0; i < randBuffer.length; ++i) {
            randBuffer[i] = RANDOM_CHARS[RandomUtils.nextInt(0, RANDOM_CHARS.length)];
        }

        return new String(randBuffer);
    }

    public static String randomNumbers(int length) {
        char[] randBuffer = new char[length];

        for(int i = 0; i < randBuffer.length; ++i) {
            randBuffer[i] = RANDOM_CHARS[RandomUtils.nextInt(0, 10)];
        }

        return new String(randBuffer);
    }

    public static String parseDomain(String requestUrl) {
        int eIdx = requestUrl.length();
        if(requestUrl.startsWith("http://")) {
            requestUrl = requestUrl.substring(7);
        } else if(requestUrl.startsWith("https://")) {
            requestUrl = requestUrl.substring(8);
        }

        eIdx = requestUrl.indexOf("/");
        return requestUrl.substring(0, eIdx);
    }

    public static String splitConcatString(String src, int len, String split) {
        if(!StringUtils.isEmpty(src) && src.length() > len) {
            int leftLen = src.length();
            int idx = 0;
            StringBuilder sb = new StringBuilder();

            for(int block = len; leftLen > 0; block = Math.min(leftLen, len)) {
                sb.append(src.substring(idx * len, idx * len + block)).append(split);
                ++idx;
                leftLen -= len;
            }

            return sb.toString();
        } else {
            return src;
        }
    }

}
